package com.rajesh.messageservice.helper;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

import java.io.File;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class FileHelper {

    public static boolean uploadFile(String serverUrl, List<String> uploadFiles, HashMap<String, String> parameters) {
        File[] targetFiles = new File[uploadFiles.size()];
        List<Part> parts = new ArrayList<>();
        PostMethod postMethod = null;
        try {
            for (int i = 0; i < uploadFiles.size(); i ++){
                targetFiles[i] = new File(uploadFiles.get(i));
                if(targetFiles[i].exists()){
                    parts.add(new FilePart(targetFiles[i].getName(), targetFiles[i]));
                }
            }

            postMethod = new PostMethod(serverUrl);
            for (String key: parameters.keySet()) {
                parts.add(new StringPart(key, parameters.get(key)));
            }
            Part[] partArray = new Part[parts.size()];
            postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(partArray), postMethod.getParams()));

            HttpClient httpClient = new HttpClient();
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            int status = httpClient.executeMethod(postMethod);
            return status == HttpStatus.SC_OK;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            if(postMethod != null)
                postMethod.releaseConnection();
        }
    }

    public static String findFile(String fileName, String basePath){
        File rootFile = new File(basePath);
        if(!rootFile.exists())
            return null;

        File[] files = rootFile.listFiles();
        Arrays.sort(files, new FileComparator());
        for (File file: files) {
            if(file.isDirectory()){
                String absolutePath = findFile(fileName, file.getAbsolutePath());
                if(absolutePath == null)
                    continue;

                return absolutePath;
            }else{
                if(file.getName().equals(fileName)){
                    return file.getAbsolutePath();
                }
            }
        }
        return null;
    }

    static class FileComparator implements Comparator {
        @Override
        public int compare(Object o1, Object o2) {
            File arg1 = (File)o1;
            File arg2 = (File)o2;
            Long arg1LastModified = arg1.lastModified();
            Long arg2LastModified = arg2.lastModified();
            return arg1LastModified.compareTo(arg2LastModified);
        }
    }
}
